﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace exsportance.Silverlight.Forms.Helpers
{
    /// <summary>
    /// Helper class to do some commanding stuff
    /// by Nick Kramer
    /// </summary>
    public class CommandHelper
    {
        #region members
        private UIElement _owner = null;
        private List<Command> _commands = null;
        #endregion

        #region constructor
        /// <summary>
        /// Creates a new command helper for an element
        /// </summary>
        /// <param name="owner"></param>
        public CommandHelper(UIElement owner)
        {
            _commands = new List<Command>();
            _owner = owner;
            owner.KeyDown += new KeyEventHandler(keyDown);
        }
        #endregion

        #region public methods

        /// <summary>
        /// Add anew command
        /// </summary>
        /// <param name="command"></param>
        public void AddCommand(Command command)
        {
            _commands.Add(command);

            // KeyBinding insists that ModifierKeys != 0 for alphabetic keys,
            // so we have to roll our own
            //this.CommandBindings.Add(new CommandBinding(command));
            //KeyGesture gesture = new KeyGesture(command.Key, command.ModifierKeys);
            //this.InputBindings.Add(new KeyBinding(command, gesture));

            if (command.Button != null)
            {
                string text = command.Text + shortcutText(command);
                ToolTip tooltip = new ToolTip();
                tooltip.Content = text;
                tooltip.Background = (Brush)Application.Current.Resources["menuBackground"];
                tooltip.Foreground = (Brush)Application.Current.Resources["menuForeground"];
                tooltip.BorderBrush = (Brush)Application.Current.Resources["shotclockBrush"];
                command.Button.Click += (object sender, RoutedEventArgs e) =>
                {
                    (command as ICommand).Execute(null);
                };
            }
        }
        #endregion

        #region private methods
        private void keyDown(object sender, KeyEventArgs e)
        {
            foreach (Command command in _commands)
            {
                // Intentionally ignore modifier keys
                bool shiftKeyMatches = (command.ModifierKeys & ModifierKeys.Shift) == (Keyboard.Modifiers & ModifierKeys.Shift);
                if (command.Key == e.Key && shiftKeyMatches)
                {
                    (command as ICommand).Execute(null);
                }
            }
        }

        private static string shortcutText(Command command)
        {
            string text = "";
            string keyText = null;
            if (command.DisplayKey != null)
                keyText = command.DisplayKey;
            else if (command.Key != Key.None)
            {
                keyText = command.Key.ToString();
                if ((command.ModifierKeys & ModifierKeys.Shift) != 0)
                    keyText = "shift+" + keyText;
            }

            if (keyText != null)
                text += " (" + keyText + ")";
            return text;
        }
        #endregion
    }



}
